package com.marquisx.common.utils;


import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

public class TZUtils
{
	private static final String TAG = "TZUtils";
	
	public static boolean conditionCheck(Context context, boolean passed, String msg )
	{
		if( !passed) 
		{//condition check failed
			Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
			return false;
		}
		
		//condition check pass
		return true;
	}
	
	public static boolean conditionCheck(Context context, boolean passed, int msgId )
	{
		return conditionCheck(context, passed, context.getResources().getString(msgId));
	}
	
	public static boolean conditionCheck(Context context, boolean passed, int msgId, int...argIds)
	{
		String[] args = new String[argIds.length];
		for( int i = 0; i < argIds.length; i++ )
		{
			args[i] = context.getResources().getString(argIds[i]);
		}
		
		return conditionCheck(context, passed, context.getResources().getString(msgId), args);
	}
	
	public static boolean conditionCheck(Context context, boolean passed, String msg, String...args)
	{
		return conditionCheck(context, passed, msg.format(msg, args));
	}
	
	public static void changeLocale(Activity activity, Locale locale)
	{
		Configuration config = new Configuration();
        config.locale = locale;
        activity.getBaseContext().getResources().updateConfiguration(config, activity.getBaseContext().getResources().getDisplayMetrics());
	}
	
	public static void changeLocale(Activity activity, String localeCode)
	{
		String[] localeArray = localeCode.split("_");
		
		assert ( localeArray.length == 2 );
		
		Locale locale;
		
		if( localeArray.length == 2 )
		{
			locale = new Locale(localeArray[0], localeArray[1]);
		}
		else
		{
			locale = new Locale(localeArray[0]);
		}
		
        Locale.setDefault(locale);
        
        changeLocale(activity, locale);
	}
	
	public static int[] getDeviceResolution(Context context)
	{
		 int width = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
		 int height = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();
		 
		 return new int[]{width, height};
	}
	
	/*
	 * =========================GRAPHIC UTILS==============================================
	 */

	/**
	 * 
	 * @param b
	 * @param degrees
	 * @return
	 */
	public static Bitmap rotate(Bitmap b, int degrees)
	{
		if (degrees != 0 && b != null)
		{
			Matrix m = new Matrix();
			m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
			try
			{
				Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
				if (b != b2)
				{
					b.recycle(); // release the resources of the bitmap
					b = b2;
				}
			}
			catch (OutOfMemoryError ex)
			{
				Log.e(TAG, ex.getMessage());
				return b;	//return the original bitmap
			}
		}
		return b;
	}
	
	public static float invSqrt(Float x)
	{
		/*
		float InvSqrt(float x)
		{
			float xhalf = 0.5f*x;
			int i = *(int*)&x; // get bits for floating VALUE 
			i = 0x5f375a86- (i>>1); // gives initial guess y0
			x = *(float*)&i; // convert bits BACK to float
			x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy
			return x;
		}  
		*/
		
		float xhalf = 0.5f*x;
		int i = x.byteValue(); // get bits for floating VALUE 
		i = 0x5f375a86 - (i>>1); // gives initial guess y0
		x = (float)(new Integer(i).byteValue()); // convert bits BACK to float
		x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy
		return x;
	}

}
